package com.taotao.controller;

import com.alibaba.fastjson.JSON;
import com.taotao.common.pojo.BaseResult;
import com.taotao.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
public class ApiSecurityController {
	private static final Logger logger = LoggerFactory.getLogger(ApiSecurityController.class);

	@Autowired
	CategoryService categoryService;
	@Autowired
	ChinaService chinaService;
	@Autowired
	PictureService pictureService;
	@Autowired
	ParamService paramService;
	@Autowired
	CleanModeService cleanModeService;
	@Autowired
	LanguageService languageService;
	@Autowired
	GoodsService goodsService;
	@Autowired
	AddressService addressService;
	@Autowired
	OrderService orderService;
	@Autowired
	TicketService ticketService;
	@Autowired
	ServiceService serviceService;
	@Autowired
	CollectService collectService;
	@Autowired
	MemberService memberService;
	@Autowired
	MessageInfoService messageInfoService;
	@Autowired
	SessionInfoService sessionInfoService;
	@Autowired
	WechatService wechatService;
	@Autowired
	WechatPayService wechatPayService;
	@Autowired
	WechatH5PayService wechatH5PayService;
	@Autowired
	ChargeService chargeService;
	@Autowired
	OperateLogService operateLogService;
	@Autowired
	NoticeService noticeService;
	@Autowired
	TaskService taskService;
	@Autowired
	TransferService transferService;
	@Autowired
	ApiSocketController apiSocketController;

	
	BaseResult baseResult = new BaseResult();

	@RequestMapping("/api/apiSecurityController")
	@ResponseBody
	public Map<String, Object> apiSecurityController(@RequestBody Map<String, String> map, HttpServletRequest request) {
		String method = map.get("method");
		logger.info(method+" 请求参数：" + map.toString());
		Map<String, Object> resultMap = baseResult.getErrorMap("网络繁忙，请稍后再试");

		if ("msgCheck".equals(method)) {  //发送get请求
			return wechatService.msgCheck(map);
		}
		
		if ("sendGet".equals(method)) {  //发送get请求
			return wechatService.sendGet(map);
		}
		
		if ("sendGetLogin".equals(method)) {  //发送get请求
			return wechatService.sendGetLogin(map);
		}
		
		if ("decodeData".equals(method)) {  //解密数据
			return wechatService.decodeData(map);
		}
		
		if ("wxLogin".equals(method)) {  //微信登录
			return memberService.wxLogin(map);
		}
		
		if ("getPicture".equals(method)) {  //获取广告图片
			return pictureService.getPicture(map);
		}
		
		if ("getParam".equals(method)) {  //获取系统参数
			return paramService.getParam(map);
		}
		
		if ("getLanguage".equals(method)) {  //获取系统语言配置
			return languageService.getLanguage(map);
		}

		if ("getAccessToken".equals(method)) {  //获取小程序的accessToken 用户敏感数据和图片校验
			return wechatService.getAccessTokenXcx(map);
		}
		
		if ("deleteSessionInfo".equals(method)) {  //删除会话
			return sessionInfoService.deleteSessionInfo(map);
		}
		
		if ("getSessionKey".equals(method)) {  //获取sessionKey
			return sessionInfoService.getSessionKey(map);
		}
		
		if ("loadMessage".equals(method)) {  //加载对话消息
			return messageInfoService.loadMessage(map);
		}
		
		if ("isReadSession".equals(method)) {  //设置会话 是否已读
			return sessionInfoService.isReadSession(map);
		}
		
		if ("getSessionList".equals(method)) {  //获取会话列表
			return sessionInfoService.getSessionList(map);
		}
		
		if ("getCategoryList".equals(method)) {  //获取分类
			return categoryService.getCategoryList(map);
		}
		
		if ("getChinaList".equals(method)) {  //获取区域
			return chinaService.getChinaList(map);
		}

		if ("loadMember".equals(method)) {  //获取会话双方会员信息
			return memberService.loadMember(map);
		}
		
		if("getMemberById".equals(method)){ //获取会员信息
			return memberService.getMemberById(map);
		}
		
		if("modifyMember".equals(method)){ //修改会员信息
			return memberService.modifyMember(map);
		}
		
		if("isColleced".equals(method)){ //获取goods是否收藏
			return collectService.isColleced(map);
		}
		
		if("goodsCollection".equals(method)){ //添加取消收藏goods
			return collectService.goodsCollection(map);
		}
		
		if("getCollectList".equals(method)){ //获取收藏的goods列表
			return goodsService.getCollectList(map);
		}
		
		if("goodsAdd".equals(method)){ //发布goods
			return goodsService.goodsAdd(map);
		}
		
		if("modifyGoods".equals(method)){ //修改goods
			return goodsService.modifyGoods(map);
		}
		
		if("getGoodsList".equals(method)){ //获取goods列表
			return goodsService.getGoodsList(map);
		}
		
		if("getGoodsInfo".equals(method)){ //获取goods详情
			return goodsService.getGoodsInfo(map);
		}
		
		if("modifyGoodsStatus".equals(method)){ //修改goods状态
			return goodsService.modifyGoodsStatus(map);
		}
		
		if("deleteGoods".equals(method)){ //删除goods
			return goodsService.deleteGoods(map);
		}
		
		if("reportGoods".equals(method)){ //举报goods
			return goodsService.reportGoods(map);
		}
		
		if("setTop".equals(method)){ //置顶
			return goodsService.setTop(map);
		}

		if("setTopCheck".equals(method)){ //置顶
			return goodsService.setTopCheck(map);
		}

		if("serviceAddOrUpdate".equals(method)){ //新增或修改服务
			return serviceService.serviceAddOrUpdate(map);
		}

		if("getServiceList".equals(method)){ //获取service列表
			return serviceService.getServiceList(map);
		}
		
		if("getServiceInfo".equals(method)){ //获取service详情
			return serviceService.getServiceInfo(map);
		}
		
		if("modifyServiceStatus".equals(method)){ //修改service状态
			return serviceService.modifyServiceStatus(map);
		}
		
		if("deleteService".equals(method)){ //删除service
			return serviceService.deleteService(map);
		}
		
		if("getOnlineStatus".equals(method)){ //查询在线状态
			return memberService.getOnlineStatus(map);
		}
		
		if("sendOfflineMessage".equals(method)){ //发送订阅离线消息
			return wechatService.sendOfflineMessage(map);
		}
		
		if("querySubscribe".equals(method)){ //查询是否关注公众号
			return wechatService.querySubscribe(map);
		}
		
		if("operateLogAdd".equals(method)){ //记录日志
			return operateLogService.operateLogAdd(map);
		}
		
		if("getNoticeList".equals(method)){ //获取通知
			return noticeService.getNoticeList(map);
		}
		
		if("addressAdd".equals(method)){ //添加地址
			return addressService.addressAdd(map);
		}
		
		if("modifyAddress".equals(method)){ //修改地址
			return addressService.modifyAddress(map);
		}
		
		if("getAddressList".equals(method)){ //获取地址列表
			return addressService.getAddressList(map);
		}
		
		if("getAddressInfo".equals(method)){ //获取地址详情
			return addressService.getAddressInfo(map);
		}
		
		if("deleteAddress".equals(method)){ //删除地址
			return addressService.deleteAddress(map);
		}
		
		if("orderConfirm".equals(method)){ //确认订单
			return orderService.orderConfirm(map);
		}
		
		if("orderAdd".equals(method)){ //添加订单
			return orderService.orderAdd(map);
		}
		
		if("modifyOrder".equals(method)){ //修改订单
			return orderService.modifyOrder(map);
		}
		
		if("deleteOrder".equals(method)){ //删除订单
			return orderService.deleteOrder(map);
		}
		
		if("modifyOrderStatus".equals(method)){ //修改订单状态
			return orderService.modifyOrderStatus(map);
		}
		
		if("getOrderList".equals(method)){ //获取订单列表
			return orderService.getOrderList(map);
		}
		
		if("getSellOrderList".equals(method)){ //获取卖家发布商品的订单列表
			return orderService.getSellOrderList(map);
		}
		
		if("getOrderInfo".equals(method)){ //获取订单详情
			return orderService.getOrderInfo(map);
		}
		
		if("getTicketList".equals(method)){ //获取卡券列表
			return ticketService.getTicketList(map);
		}
		
		if("modifyTicketStatus".equals(method)){ //更新卡券状态
			return ticketService.modifyTicketStatus(map);
		}
		
		if("closeOrder".equals(method)){ //关闭订单
			return wechatPayService.closeOrder(map);
		}
		
		if("refund".equals(method)){ //申请退款
			return wechatPayService.refund(map);
		}
		
		if("chargeAdd".equals(method)){ //充值
			return chargeService.chargeAdd(map);
		}
		
		if("getChargeList".equals(method)){ //查询充值消费记录
			return chargeService.getChargeList(map);
		}

		if("getCleanMode".equals(method)){ //获取clean mode以及价格
			return cleanModeService.getCleanMode(map);
		}

		if("deleteImages".equals(method)){ //修改图片时 删除无效的图片
			return pictureService.deleteImages(map);
		}

		if("transferAdd".equals(method)){ //修改图片时 删除无效的图片
			return transferService.transferAdd(map);
		}

		if("sendMessage".equals(method)){ //发生消息，先检查没问题在将消息丢给给socket
			return apiSocketController.sendToUser(JSON.toJSONString(map));
		}

		if("unifiedOrderH5".equals(method)){//h5统一下单
			return wechatH5PayService.unifiedOrder(map,request);
		}

		if("createJSAPI".equals(method)){// 微信浏览器内调用JSAPI支付
			return wechatPayService.createJSAPI(map);
		}

		return resultMap;
	}


	@RequestMapping("/api/getJssdkWechatConfig")
	@ResponseBody
	public Map<String, Object> getJssdkWechatConfig(@RequestBody Map<String, String> map, HttpServletRequest request) {
		logger.info("请求参数：" + map.toString());
		String method = map.get("method");
		Map<String, Object> resultMap= baseResult.getErrorMap("网络繁忙，请稍后再试");

		if ("getJssdkWechatConfig".equals(method)) {
			return wechatService.getJssdkWechatConfig(map);
		}
		return resultMap;
	}


	@RequestMapping("/api/uploadPicture")
	@ResponseBody
	public Map<String, Object> uploadPicture(HttpServletRequest request) {

		Map<String, Object> resultMap = baseResult.getErrorMap("网络繁忙，请稍后再试");
		resultMap=pictureService.uploadPicture(request);

		return resultMap;
	}
}
